From 333af13610cf3194c15d6a96bfc8f3820f04bb5e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 29 Oct 2014 16:27:16 -0700 Subject: [PATCH] Check USERNAME for a users's name with `cargo new` Closes #740 --- src/cargo/ops/cargo_new.rs | 10 +++++++--- tests/test_cargo_new.rs | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index d5c0d18ef..1fc5ab7d0 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -111,11 +111,15 @@ fn discover_author() -> CargoResult<(String, Option)> { let git_config = git_config.as_ref(); let name = git_config.and_then(|g| g.get_str("user.name").ok()) .map(|s| s.to_string()) - .or_else(|| os::getenv("USER")); + .or_else(|| os::getenv("USER")) // unix + .or_else(|| os::getenv("USERNAME")); // windows let name = match name { Some(name) => name, - None => return Err(human("could not determine the current user, \ - please set $USER")) + None => { + let username_var = if cfg!(windows) {"USERNAME"} else {"USER"}; + return Err(human(format!("could not determine the current \ + user, please set ${}", username_var))) + } }; let email = git_config.and_then(|g| g.get_str("user.email").ok()); diff --git a/tests/test_cargo_new.rs b/tests/test_cargo_new.rs index 7539a0475..79694d56e 100644 --- a/tests/test_cargo_new.rs +++ b/tests/test_cargo_new.rs @@ -120,6 +120,21 @@ test!(finds_author_user { assert!(toml.as_slice().contains(r#"authors = ["foo"]"#)); }) +test!(finds_author_username { + // Use a temp dir to make sure we don't pick up .cargo/config somewhere in + // the hierarchy + let td = TempDir::new("cargo").unwrap(); + assert_that(cargo_process("new").arg("foo") + .env("USER", None::<&str>) + .env("USERNAME", Some("foo")) + .cwd(td.path().clone()), + execs().with_status(0)); + + let toml = td.path().join("foo/Cargo.toml"); + let toml = File::open(&toml).read_to_string().assert(); + assert!(toml.as_slice().contains(r#"authors = ["foo"]"#)); +}) + test!(finds_author_git { my_process("git").args(["config", "--global", "user.name", "bar"]) .exec().assert(); -- 2.30.2